<?php
/**
 * 描述
 * Created by Paco
 * Date: 2018/11/19 Time: 16:21
 */

namespace App\Repositories\Order;


use App\Lib\Utils\Cart;
use App\Models\Product\Product;
use App\Models\Product\Sku;
use App\Repositories\Repository;

class CartRepository extends Repository
{

    public function carts()
    {
        $user = $this->getUser();
        $cart = Cart::getCar($user->user_id);
        $cartItems = $cart->listItems();

        $pro_ids = [];
        $sku_ids = [];
        foreach ($cartItems as $cartItem) {
            $pro_ids[] = $cartItem['product_id'];
            if ($cartItem['sku_id']) {
                $sku_ids[] = $cartItem['sku_id'];
            }
        }

        $pro_data = $this->getProducts($pro_ids);
        $sku_data = $this->getSkus($sku_ids);

        foreach ($cartItems as $k => $cartItem) {
            $cartItems[$k]['product'] = $pro_data[$cartItem['product_id']];
            if ($cartItem['sku_id']) {
                $cartItems[$k]['sku'] = $sku_data[$cartItem['sku_id']];
            }
        }

        return $cartItems;
    }


    public function totalCount()
    {
        $user = $this->getUser();
        return Cart::getCar($user->user_id)->getCount();
    }


    public function count()
    {
        $user = $this->getUser();
        return Cart::getCar($user->user_id)->cnt();
    }


    public function addItem($product_id, $sku_id)
    {
        try {
            $user = $this->getUser();
            Cart::getCar($user->user_id)->addItems($product_id, $sku_id);
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }


    public function delItem($product_id, $sku_id)
    {
        try {
            $user = $this->getUser();
            Cart::getCar($user->user_id)->delItems($product_id, $sku_id);
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }


    public function increment($product_id, $sku_id)
    {
        try {
            $user = $this->getUser();
            Cart::getCar($user->user_id)->addOne($product_id, $sku_id);
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

    public function decrement($product_id, $sku_id)
    {
        try {
            $user = $this->getUser();
            Cart::getCar($user->user_id)->delOne($product_id, $sku_id);
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

    public function saveItem($product_id, $sku_id, $number)
    {
        try {
            $user = $this->getUser();
            Cart::getCar($user->user_id)->upItems($product_id, $sku_id, $number);
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

    public function clear()
    {
        try {
            $user = $this->getUser();
            Cart::getCar($user->user_id)->clear();
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }

    public function sync()
    {
        try {
            $user = $this->getUser();
            Cart::getCar($user->user_id)->syncCart();
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }


    private function getProducts($pro_ids)
    {
        $products = Product::query()->whereIn('product_id', $pro_ids)->get()->toArray();
        $pro_data = [];
        foreach ($products as $product) {
            $pro_data[$product['product_id']] = $product;
        }
        return $pro_data;
    }


    private function getSkus($sku_ids)
    {
        $sku_data = [];
        $skus = Sku::query()->whereIn('spec_ids', $sku_ids)->get()->toArray();
        foreach ($skus as $sku) {
            $sku_data[$sku['spec_ids']] = $sku;
        }
    }
}